<?php
namespace common\service;
use common\models\AttributeValue;
use common\models\ActiveAttr;
use common\models\GroupItem;
use common\models\Order;
use common\models\GoodsAttr;
use common\models\Active;
class ActiveService extends BaseService
{
    /**
     * 活动SKU
     * $type 1 过滤库存0
     * @param $id
     * @return mixed
     */
    public static function GetAttrlist($id)
    {
        $lists = ActiveAttr::find();
        $lists->andWhere(['active_id' => $id, 'is_del' => 0]);

        $list = $lists->asArray()->all();

        if ($list) {
            $aid = explode('-', $list[0]['attribute_value_id']);
            $attrtitle = AttributeValue::find()->where(['id' => $aid])->orderBy(['id' => SORT_ASC])->asArray()->all();
            $vid = array_column($list, 'attribute_value_id');

            $newdata['thead'] = array_column($attrtitle, 'attr_name');
            $newiddata = [];
            foreach ($vid as $val) {
                $newid = explode('-', $val);
                foreach ($newid as $vid) {
                    if ($vid > 0) {
                        $newiddata[] = $vid;
                    }
                }
            }
            $newiddata = array_unique($newiddata);

            $newvaldata = AttributeValue::find()->where(['in', 'id', $newiddata])->asArray()->all();
            $valdatanew = array();
            foreach ($newvaldata as $newdats) {
                $valdatanew[$newdats['id']] = $newdats;
            }


            $data = [];
            foreach ($list as $lval) {
                $newavid = explode('-', $lval['attribute_value_id']);
                $newvalue['original_price'] = $lval['original_price'];
                $newvalue['collage_price'] = $lval['collage_price'];
                $newvalue['one_price'] = $lval['one_price'];
                $newvalue['inventory'] = $lval['inventory'];
                $newvalue['image'] = GlobalUrlService::buildCdnUrl($lval['attr_img']);
                $newvalue['attr_img'] = $lval['attr_img'];
                $newvalue['attribute_value_id'] = $lval['attribute_value_id'];
                $newvalue['active_id'] = $lval['active_id'];
                $newvalue['goods_attre_id'] = $lval['goods_attre_id'];
                $newvalue['sales_volume'] = $lval['sales_volume'];
                $newvalue['display_sales'] = $lval['display_sales'];
                $newvalue['id'] = $lval['id'];
                $namedata = array();
                $attrvaldata = array();
                foreach ($newavid as $newval) {
                    if ($newval > 0) {
                        $namedata[] = $valdatanew[$newval];
                        $attrvaldata[] = $valdatanew[$newval]['value'];
                    }
                }
                $newvalue['attr_value'] = implode(':', $attrvaldata);
                $newvalue['val'] = $namedata;
                $newvalue['order_count'] = 0;
                $item['order_count'] = 0;
                $data[] = $newvalue;
            }
            $newdata['tbody'] = $data;
            return $newdata;
        } else{
            return array();
        }
    }


    /**
     * 删除活动返回商品库存
     * @param $id
     */
    public static function DelActive($id){
        $re = Active::findOne($id);
        $avtiveAttr = ActiveAttr::find()->where(['active_id'=>$id])->asArray()->all();
        $goodsAttr = GoodsAttr::find()->where(['goods_id'=>$re['goods_id']])->asArray()->all();

        $adata = [];
        foreach ($avtiveAttr as $aattr){
            $adata[$aattr['goods_attre_id']] = $aattr;
        }

        foreach ($goodsAttr as $battr){
            if(isset($adata[$battr['id']])) {
                $goodsdata = GoodsAttr::findOne(['id' => $battr['id']]);
                $goodsdata->inventory = $battr['inventory'] + $adata[$battr['id']]['inventory'];
                $goodsdata->update_time = time();
                $goodsdata->save();
            }
        }
        return true;
    }
    /**
     * 最高最低价格
     * @param $data
     * @return mixed
     */
    public static function GetPrice($data){
        if (count($data) == count($data, 1)) {
            $id = $data['id'];
        } else {
            $id = array_column($data,'id');
        }

        $attr = ActiveAttr::find()->select('active_id,max(original_price) as max,min(collage_price) as min')
            ->where(['active_id'=>$id])
            ->groupBy('active_id')
            ->asArray()
            ->all();
        $goodsCount = Order::find()->select('sum(goods_num) as goods_num,active_id')->where(['active_id' => $id, 'pay_status' => 1])->groupBy('active_id')->asArray()->all();
        $goodsSum = [];
        foreach ($goodsCount as $count){
            $goodsSum[$count['active_id']] = $count;
        }
        if (count($data) == count($data, 1)) {
            $data['max_price'] = $attr[0]['max'];
            $data['min_price'] = $attr[0]['min'];
        } else {
            $newattr = [];
            foreach ($attr as $val){
                $newattr[$val['active_id']] = $val;
            }

            unset($val);
            foreach ($data as &$price){
                if(isset($goodsSum[$price['id']]) && !empty($goodsSum[$price['id']])){
                    $price['sales_volume'] = $goodsSum[$price['id']]['goods_num'];
                } else {
                    $price['sales_volume'] = 0;
                }
                $price['max_price'] = $newattr[$price['id']]['max'];
                $price['min_price'] = $newattr[$price['id']]['min'];
            }
        }
        return $data;
    }
}